package cn.edu.hunau.basic.day05;

import org.junit.Test;

import java.util.Arrays;

/**
 * @author hb
 * @date 2025-08-01 09:18:25
 */
public class SelectSortHw {
    @Test
    public void test02(){
      int[] arr={3,5,2,6,7,1,0,10,9};
      selectSort(arr);
      System.out.println(Arrays.toString(arr));
    }

    @Test
    public void test03(){
        int[] arr={3,5,2,6,7,1,0,10,9};
        selectSort2(arr);
        System.out.println(Arrays.toString(arr));
    }

    public void selectSort2(int[] arr){
        for (int i=0;i<arr.length-1;i++){
            int index=i;
            for(int j=i+1;j<arr.length;j++){
                if(arr[j]<arr[index]){
                     index=j;
                }
            }
            swap(arr,i,index);
        }
    }






    public void selectSort(int[] arr){
        for(int i=0;i<arr.length-1;i++){
            int temp =arr[i+1];
            int j=i+1;
            int index=j; //index作为最小值得下标

            //获得j范围内的最小值
            for(;j<arr.length;j++){
                 if(arr[j]<temp){
                    temp=arr[j];
                    index=j;
                 }
            }
            System.out.println("最小值"+temp);
            if(temp<arr[i]){
                  swap(arr,i,index);
                  System.out.println("\t"+i+":"+index+","+Arrays.toString(arr));
            }
        }
    }

    public void swap(int[] arr,int i,int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
